<?php

namespace Admin\Controller;

use Common\Controller\BaseController;

/**
 * 后台Ajax请求控制器类
 * 
 * 不继承后台基类CommonController
 * 因此在该类中所有的操作不受权限控制
 * 
 * @author: xuhui <xuh86@126.com>
 * @version: $Id$
 * @package: asoc.Admin.Controller
 * 
 */


class AjaxController extends BaseController {
	
	
	/**根据分类id获取下级分类
	 * 
	 */
	public function getSubCateList()
    {
        $cateNum = 0;
        $cateList =	NULL;
        $attrHtmls = "";
		$id		=	$this->getInt('id');
		if($id)
        {
            //下级分类
			$subCateResult	=	D('Category')->getItemListBy(array('parent_id'=>$id),0,0);
			$cateNum = $subCateResult['count'];
			$cateList =	$subCateResult['items'];

            //获取分类下的属性
            $group_id = M('GoodsAttrgrp')->where("`category_id`={$id}")->getField('group_id');
            if($group_id > 0)
            {
                $attrHtmls = $this->getAttrSelHtml($group_id,null);
            }
		}
		
		echo json_encode(array('cateNum'=>$cateNum,'cateList'=>$cateList,'attrHtmls'=>$attrHtmls));
	}




    private function getAttrSelHtml($group_id=0,$attrs=array()){

        $attrHtmls	=	'';
        if($group_id > 0){
            $aModel	=	D('Attr');
            $gav	=	M('GoodsAttrVal');
            $gga	=	M('GoodsGroupAttr');
            $attrList	=	$gga->where("`group_id`={$group_id}")->select();
            foreach ($attrList as $key=>$val){
                $attr	=	$aModel->getItemById($val['attr_id']);

                $seled	=	false;		//该属性是否已选中
                $selVal	=	'';			//已选中属性值
                if($attrs != NULL){
                    for($i=0;$i < count($attrs);$i++){
                        $temp = explode('___', $attrs[$i]);
                        if(intval($temp[1]) == $val['attr_id']){
                            $seled	=	true;
                            $selVal = $temp[0];
                            break;
                        }
                    }
                }
                if(!$seled){
                    $attrHtmls	.='<div class="attrSel" onmouseover="triggerSel(this,true)"  onmouseout="triggerSel(this,false)">
								<a class="attribute" href="javascript:void(0);" name="'.$val['attr_id'].'">'.$attr['attr_name'].'</a>
								<div class="hide-border"></div>
								<ul>';
                }else{
                    $attrHtmls	.='<div class="attSeled" >
								<span>'.$selVal.'</span><a class="btn-close" href="javascript:void(0);" onclick="attrValSel(this,false)"></a>
								<a class="attribute" style="display:none;" href="javascript:void(0);" name="'.$val['attr_id'].'">'.$attr['attr_name'].'</a>
								<div class="hide-border" style="display:none;"></div>
								<ul  style="display:none;">';
                }
                //获取该属性所有的值
                $attrVals	=	$gav->where("`attr_id`={$val['attr_id']} AND `group_id`={$group_id}")->field('attr_val')->select();
                foreach ($attrVals as $k=>$v){
                    $attrHtmls	.='<li><a href="javascript:void(0);"  onclick="attrValSel(this,true)">'.$v['attr_val'].'</a></li>';
                }
                $attrHtmls	.='</ul></div>';
            }
        }
        return $attrHtmls;
    }





	
	/**
	 * 根据分组id返回该分组下面所有属性
	 */
	public function getGroupAttrList(){
		
		$count	=	0;
		$attrList=	NULL;
		$id		=	$this->getInt('id');		//分组id
		$goods_id =	$this->getInt('gid');
		if($id > 0){
			
			$attrModel	=	D('Attr');
			$attrList	=	$attrModel->getGroupAttrList($id);
			if($attrList){
				foreach ($attrList as $key=>$val){
					$attrList[$key]['html']	=	$attrModel->getAttrFromInput($val['attr_id'],$goods_id);
				}
				$count		=	count($attrList);
			}
		}
		
		echo json_encode(array('count'=>$count,'data'=>$attrList));
	}
	
	//删除已上传商品图片
	public function delGoodsImg(){
		
		$img_id		=	$this->getInt('imgId');		
		$flag		=	0;
		if($img_id>0){
			if(D('GoodsGallery')->delImgById($img_id))	$flag	=	1;
		}
		
		echo $flag;
	}
	

	/**
	 * 获取指定语言的信息
	 */
	public function getLangInfo(){
	
		$pkId	=	$this->getInt('pk');
		$table	=	$this->getString('table');
		$lang	=	$this->getString('lang');
	
		$info	=	NULL;
		if($pkId > 0 && $table != "" && $lang != ""){
			$langShort	=	getLangShort($lang);
				
			$baseModel	=	M($table);
			$infoModel	=	M($table."Info");
				
			$pk		=	$baseModel->getPk();
			$map[$pk]	=	$pkId;
			$map['lang']	=	$langShort;
				
			$info	=	$infoModel->where($map)->find();
			if(!$info && $langShort != "cn"){
				$map['lang']	=	'cn';
				$info	=	$infoModel->where($map)->find();
			}
		}
		$status	=	$info ? 1 : 0;
		$array	=	array('status'=>$status,'data'=>$info);
	
		echo json_encode($array);
	}
	
	
}

?>